package com.yanyuan.algorithm.map;

import com.alibaba.fastjson.JSONObject;
import com.yanyuan.algorithm.util.Print;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yanyuan
 * @Date: 2021/3/25 10:36
 * @Description:
 */
@Slf4j
public class MapOperation {

    static Map<Integer, String> map = null;
    static {
        map = new HashMap<>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
    }

    /**
     * putIfAbsent 方法
     *  如果key关联的value不存在，则关联新的value值，返回key关联的旧的值
     */
    public static void putIfAbsent(){
        map.putIfAbsent(3, "d");
        map.putIfAbsent(4, "d");
        Print.msg("map = {0}, size = {1}", JSONObject.toJSON(map), map.size());
    }

    public static void main(String[] args) {
        MapOperation.putIfAbsent();
    }
}
